package com.sp.exit_;

/**
 * 线程终止
 * 启动一个线程t, 要求在main函数中去停止线程t
 *
 * 如果希望main线程去控制t1 线程的终止, 必须可以修改 loop
 * 让t1 退出run方法，从而终止 t1线程 -> 通知方式
 */
public class ThreadExit {
    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        t1.start();

        //让主线程休眠10秒, 再通知t1线程退出
        Thread.sleep(10 * 1000);
        t1.setLoop(false);

    }
}

class T extends Thread {
    private int count = 0;
    private boolean loop = true;
    @Override
    public void run() {
        while (loop){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("T 运行中...." + (++count));
        }
    }

    public void setLoop(boolean loop){
        this.loop = loop;
    }
}